package booksys.bod;

import booksys.common.CurrentUser;
import booksys.common.Utils;
import org.apache.ibatis.session.SqlSession;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class BodService {
    public List<Bodcard> getNewBodcardList(CurrentUser currentUser) {
        SqlSession sess = Utils.openSession();
        try {
            BodDao dao = sess.getMapper(BodDao.class);
            List<Bodcard> bodcardList = dao.findNewBodcardList(currentUser.getUserId());//拿到集合
            sess.commit();//提交事务

            return bodcardList;
        } catch (Exception e) {
            sess.rollback();//出现异常回滚事务
            e.printStackTrace();
            throw new RuntimeException("获取申请列表失败！", e);

        } finally {
            sess.close();//关闭会话,否则其他用户无法连接数据库
        }

    }

    public void addBodcard(BodcardDto dto, String userId) {
        dto.setSt_id(userId);

        SqlSession sess = Utils.openSession();
        try {
            BodDao dao = sess.getMapper(BodDao.class);
            dto.setBob_id(dao.finBobid(dto));

            boolean hasbook = dao.findbook(dto.getBb_id());
            if (!hasbook) {
                throw new RuntimeException("书号输入错误！书库中无此书");
            }

            boolean beborrow = dao.beborrow(dto.getBb_id());
            if (!beborrow) {
                throw new RuntimeException("书号输入错误！此书未被借出");
            }

            if(!dao.getst_id(dto).equals(dto.getSt_id())){
                throw new RuntimeException("书号输入错误！此书已被其他同学借阅");
            }

            if(dao.getbd_up(dto)!=0){
                throw new RuntimeException("该书借阅缓期申请已提交，请等候管理员处理");
            }

            if(dao.getbd_upxx(dto)!=0){
                throw new RuntimeException("该书借阅缓期申请已通过，无法继续申请");
            }

            java.util.Calendar cal = java.util.Calendar.getInstance();

            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;

            String mom = month < 10 ? ("0" + month) : ("" + month);

            String newNum = dao.findBodcardNewNum(year + mom);

            dto.setBd_id(year + mom + newNum);
            dto.setBd_time(cal.getTime());

            String bob_extime=dao.getbob_extime(dto);
            String[] bob=bob_extime.split("-");
            int Year =Integer.parseInt(bob[0]);
            int Month =Integer.parseInt(bob[1]);
            int Day = Integer.parseInt(bob[2]);
            cal.set(Year,Month-1,Day);
            dto.setBd_extime(cal.getTime());
            cal.add(Calendar.DATE, 7);
            dto.setBd_defer(cal.getTime());


            dto.setMa_id(dao.findManagerByBobid(dao.finBobid(dto)));
            dto.setBd_up(1);
            dto.setBd_agree(0);

            dao.insertBobcard(dto);

            sess.commit();

        } catch (Exception e) {
            sess.rollback();
            if (e instanceof RuntimeException) {
                throw e;
            }
            throw new RuntimeException("新增借阅缓期申请失败！", e);


        } finally {
            sess.close();
        }
    }
}
